home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group93c.txt
/
000127_icon-group-sender _Tue Dec 14 00:16:09 1993.msg
< prev
next >
Wrap
Internet Message Format
|
1994-02-02
|
3KB
Received: by cheltenham.cs.arizona.edu; Thu, 16 Dec 1993 23:05:50 MST
Date: 14 Dec 93 00:16:09 GMT
From: organpipe.uug.arizona.edu!CS.Arizona.EDU!not-for-mail@uunet.uu.net (Nevin Liber)
Organization: University of Arizona CS Department, Tucson AZ
Subject: Re: Need help with (simple?) programming problem.
Message-Id: <2ej0k9$5jg@caslon.CS.Arizona.EDU>
References: <rfgCHyt15.Fpz@netcom.com>
Sender: icon-group-request@cs.arizona.edu
To: icon-group@cs.arizona.edu
Status: R
Errors-To: icon-group-errors@cs.arizona.edu
In article <rfgCHyt15.Fpz@netcom.com>,
Ronald F. Guilmette <rfg@netcom.com> wrote:
|Assume that I have three strings, i.e. "red ", "green ", and "blue ". Now
|I want to create a generator which will successively yield each unique
|string which contains no more than one instance of each of the original
|strings. Here are the string values which should be yielded by the various
|invocations/resumptions of the generator:
|
| ""
| "red "
| "green "
| "blue "
| "red green "
| "red blue "
| "green red "
| "green blue "
| "blue red "
| "blue green "
| "red green blue "
| "red blue green "
| "green red blue "
| "green blue red "
| "blue red green "
| "blue green red "
|
|After being invoked/resumed 16 times (and yielding each of the above strings
|once) the generator should fail. Note that the actual *order* in which the
|above strings are yielded is totally unimportant. Any order will do as long
|as I get these 16 strings (eventually).
|Also note that the ORDER in which the original strings appear in the genera-
|ted strings *is* significant.
Actually, it isn't. You are just performing a permutation of all
possible strings, so the order that the original strings appear in
shouldn't matter.
Anyway, here is a piece of code to do it:
procedure main(LArguments)
local SColors
SColors := set(["red", "green", "blue" ])
every write(image(Permute(SColors)))
end
procedure Permute(SSet)
local sString
every sString := !SSet do {
every suspend sString || " " || Permute(delete(copy(SSet), sString))
}
return ""
end
and it produces:
"blue red green "
"blue red "
"blue green red "
"blue green "
"blue "
"red blue green "
"red blue "
"red green blue "
"red green "
"red "
"green blue red "
"green blue "
"green red blue "
"green red "
"green "
""
I can't remember if I came up with this algorithm on my own or if I
found it in the Icon Program Library (I can't seem to find the IPL on
this machine), but I do recall posting an explanation of how this
works about a year ago. [Check the archives of the icon group mailing
list / comp.lang.icon on cs.arizona.edu for more info.]
|(In practice, for
|my *real* problem, the number of original strings will in fact be larger
|than three... but it *will* be a fixed number (probably less that 12).
Actually, the number of strings doesn't matter.
__
Nevin ":-)" Liber nevin@cs.arizona.edu (602) 293-2799
^^^ (520) after 3/95
--
Nevin ":-)" Liber nevin@cs.arizona.edu (602) 293-2799
^^^ (520) after 3/95